package com.spzx.user.controller;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.spzx.common.core.domain.R;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.user.domain.UserAddress;
import com.spzx.user.domain.UserRegisterDTO;
import com.spzx.user.domain.vo.UserInfoVo;
import jakarta.servlet.http.HttpServletResponse;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.user.domain.UserInfo;
import com.spzx.user.service.IUserInfoService;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.spzx.common.core.web.page.TableDataInfo;

/**
 * 会员Controller
 *
 * @author atguigu
 * @date 2024-09-29
 */
@Tag(name = "会员接口管理")
@RestController
@RequestMapping("/userInfo")
public class UserInfoController extends BaseController
{
    @Autowired
    private IUserInfoService userInfoService;


    @Operation(summary = "查询登录用户的个人信息")
//com.spzx.common.core.domain.R<com.spzx.user.api.domain.UserInfo>
    @GetMapping("getLoginUserInfo")
    public AjaxResult getLoginUserInfo(){
        UserInfo userInfo = userInfoService.getById(SecurityUtils.getUserId());
        //不能直接使用userInfo对象返回，前端项目存在bug，如果返回的数据包含了密码字段 前端不会缓存用户信息
        UserInfoVo vo = new UserInfoVo();
        vo.setAvatar(userInfo.getAvatar());
        vo.setNickName(userInfo.getNickName());

        return success(vo);
    }


    @InnerAuth
    @Operation(summary = "内部调用接口：登录根据账号查询会员数据")
//com.spzx.common.core.domain.R<com.spzx.user.api.domain.UserInfo>
    @PostMapping("getUserInfo")
    public R<UserInfo> getUserInfo(@RequestParam("username") String username){
        return R.ok(userInfoService.getOne(Wrappers.lambdaQuery(UserInfo.class)
                .eq(UserInfo::getUsername , username)
                .last("limit 1")));
    }

    @InnerAuth
    @Operation(summary = "内部调用接口：注册保存会员数据")
    @PostMapping("saveUserInfo")
    public R saveUserInfo(@RequestBody UserRegisterDTO userRegisterDTO){
        userInfoService.saveUserInfo(userRegisterDTO);
        return R.ok();
    }



    /**
     * 查询会员列表
     */
    @Operation(summary = "查询会员列表")
    @RequiresPermissions("user:userInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(UserInfo userInfo)
    {
        startPage();
        List<UserInfo> list = userInfoService.selectUserInfoList(userInfo);
        return getDataTable(list);
    }

//    addressList

    @Operation(summary = "根据userid查询会员地址详细信息")
    @RequiresPermissions("user:userInfo:list")
    @GetMapping("/addressList")
    public AjaxResult addressList(Long id)
    {
        List<UserAddress> list = userInfoService.selectAddressList(id);
        return success(list);
    }
    /**
     * 导出会员列表
     */
    @Operation(summary = "导出会员列表")
    @RequiresPermissions("user:userInfo:export")
    @Log(title = "会员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserInfo userInfo)
    {
        List<UserInfo> list = userInfoService.selectUserInfoList(userInfo);
        ExcelUtil<UserInfo> util = new ExcelUtil<UserInfo>(UserInfo.class);
        util.exportExcel(response, list, "会员数据");
    }

    /**
     * 获取会员详细信息
     */
    @Operation(summary = "获取会员详细信息")
    @RequiresPermissions("user:userInfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userInfoService.getById(id));
    }

    /**
     * 新增会员
     */
    @Operation(summary = "新增会员")
    @RequiresPermissions("user:userInfo:add")
    @Log(title = "会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserInfo userInfo)
    {
        return toAjax(userInfoService.save(userInfo));
    }

    /**
     * 修改会员
     */
    @Operation(summary = "修改会员")
    @RequiresPermissions("user:userInfo:edit")
    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserInfo userInfo)
    {
        return toAjax(userInfoService.updateById(userInfo));
    }

    /**
     * 删除会员
     */
    @Operation(summary = "删除会员")
    @RequiresPermissions("user:userInfo:remove")
    @Log(title = "会员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userInfoService.removeBatchByIds(Arrays.asList(ids)));
    }
}
